package com.csyaonie.android.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.csyaonie.android.R;

public class FragmentActivity extends AppCompatActivity implements AFragment.SendMessage {

    private AFragment aFragment;
    private BFragment bFragment;
    private Button fbt1;
    private TextView ftv0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        aFragment=AFragment.newInstance("我是参数");
        getSupportFragmentManager().beginTransaction().add(R.id.fl,aFragment,"a").commitAllowingStateLoss();
        fbt1=findViewById(R.id.fbt1);
        ftv0=findViewById(R.id.ftv0);
        fbt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bFragment==null){
                    bFragment=new BFragment();
                }
                Fragment fragment=getSupportFragmentManager().findFragmentByTag("a");
                if(fragment!=null){
                    getSupportFragmentManager().beginTransaction().hide(fragment).add(R.id.fl,bFragment).addToBackStack(null).commitAllowingStateLoss();
                }else{
                    getSupportFragmentManager().beginTransaction().replace(R.id.fl,bFragment).addToBackStack(null).commitAllowingStateLoss();
                }

            }
        });
    }

    public void setData(String title){
        ftv0.setText(title);
    }

    @Override
    public void onClick(String title) {
        ftv0.setText(title);
    }
}